package jaux.tank.factory;

import org.springframework.beans.BeanUtils;

import jaux.tank.bean.dto.UserDto;
import jaux.tank.bean.entity.system.User;
import jaux.tank.bean.vo.UserInfo;
import jaux.tank.bean.vo.UserSettings;
import jaux.tank.utils.MD5;
import jaux.tank.utils.StringUtils;
import jaux.tank.utils.ToolUtil;

/**
 * 用户创建工厂
 *
 * @date 2017-05-05 22:43
 */
public class UserFactory {

    public static User createUser(UserDto userDto, User user){
        if(userDto == null){
            return null;
        }else{
            BeanUtils.copyProperties(userDto,user);
            return user;
        }
    }
    public static User updateUser(UserDto userDto,User user){
        if(userDto == null){
            return null;
        }else{
            user.setName(userDto.getName());
            user.setDeptid(userDto.getDeptid());
            user.setSex(userDto.getSex());
            user.setPhone(userDto.getPhone());
            user.setEmail(userDto.getEmail());
            user.setBirthday(userDto.getBirthday());
            if(userDto.getStatus()!=null){
                user.setStatus(userDto.getStatus());
            }
            if(StringUtils.isNotEmpty(userDto.getPassword())) {
	        	 user.setSalt(ToolUtil.getRandomString(5));
	             user.setPassword(MD5.md5(userDto.getPassword(), user.getSalt()));
            }
            user.setAvatar(userDto.getAvatar());
            user.setRoleid(userDto.getRoleid());
            user.setVersion(userDto.getVersion());
            return user;
        }
    }
    public static User updateUser(UserSettings userSettings, User user){
    	if(userSettings == null){
    		return null;
    	}else{
    		user.setName(userSettings.getName());
    		user.setSex(userSettings.getSex());
    		user.setPhone(userSettings.getPhone());
    		user.setEmail(userSettings.getEmail());
    		user.setAvatar(userSettings.getAvatar());
    		user.setBirthday(userSettings.getBirthday()); 
    		return user;
    	}
    }
    
    public static UserInfo getUserInfo(User user){
		if(user == null){
			return null;
		}else{
			UserInfo userInfo = new UserInfo();
			BeanUtils.copyProperties(user,userInfo);
			return userInfo;
		}
	}
}
